<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2310 – This function is invoked in an 'if' condition so set -e will be disabled. Invoke separately if failures should cause the script to exit.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2310 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2310">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="this-function-is-invoked-in-an-if-condition-so-set--e-will-be-disabled-invoke-separately-if-failures-should-cause-the-script-to-exit">This
function is invoked in an 'if' condition so set -e will be disabled.
Invoke separately if failures should cause the script to exit.</h2>
<p>(This warning is optional and must be explicitly enabled)</p>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2310.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">#!/bin/sh</span></span>
<span id="cb1-2"><a href="SC2310.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="co">#shellcheck enable=check-set-e-suppressed</span></span>
<span id="cb1-3"><a href="SC2310.html#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="SC2310.html#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="bu">set</span> <span class="at">-e</span></span>
<span id="cb1-5"><a href="SC2310.html#cb1-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-6"><a href="SC2310.html#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="fu">backup()</span> <span class="kw">{</span></span>
<span id="cb1-7"><a href="SC2310.html#cb1-7" aria-hidden="true" tabindex="-1"></a>  <span class="fu">cp</span> <span class="pp">*</span>.txt /backup</span>
<span id="cb1-8"><a href="SC2310.html#cb1-8" aria-hidden="true" tabindex="-1"></a>  <span class="fu">rm</span> <span class="pp">*</span>.txt            <span class="co"># Runs even if copy fails!</span></span>
<span id="cb1-9"><a href="SC2310.html#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span>
<span id="cb1-10"><a href="SC2310.html#cb1-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-11"><a href="SC2310.html#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="ex">backup</span></span>
<span id="cb1-12"><a href="SC2310.html#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="cf">then</span></span>
<span id="cb1-13"><a href="SC2310.html#cb1-13" aria-hidden="true" tabindex="-1"></a>    <span class="bu">echo</span> <span class="st">&quot;Backup successful&quot;</span></span>
<span id="cb1-14"><a href="SC2310.html#cb1-14" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2310.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co">#!/bin/sh</span></span>
<span id="cb2-2"><a href="SC2310.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co">#shellcheck enable=check-set-e-suppressed</span></span>
<span id="cb2-3"><a href="SC2310.html#cb2-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-4"><a href="SC2310.html#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="bu">set</span> <span class="at">-e</span></span>
<span id="cb2-5"><a href="SC2310.html#cb2-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-6"><a href="SC2310.html#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="fu">backup()</span> <span class="kw">{</span></span>
<span id="cb2-7"><a href="SC2310.html#cb2-7" aria-hidden="true" tabindex="-1"></a>  <span class="fu">cp</span> <span class="pp">*</span>.txt /backup</span>
<span id="cb2-8"><a href="SC2310.html#cb2-8" aria-hidden="true" tabindex="-1"></a>  <span class="fu">rm</span> <span class="pp">*</span>.txt</span>
<span id="cb2-9"><a href="SC2310.html#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span>
<span id="cb2-10"><a href="SC2310.html#cb2-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-11"><a href="SC2310.html#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="ex">backup</span></span>
<span id="cb2-12"><a href="SC2310.html#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;Backup successful&quot;</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>ShellCheck found a function used as a condition in a script where
<code>set -e</code> is enabled. This means that the function will run
without <code>set -e</code>, and will power through any errors.</p>
<p>This applies to <code>if</code>, <code>while</code>, and
<code>until</code> statements, commands negated with <code>!</code>, as
well as the left-hand side of <code>||</code> and
<code>&amp;&amp;</code>. It does not matter how deeply the command is
nested in such a structure.</p>
<p>In the problematic example, the intent was that an error like
<code>cp: error writing '/backup/important.txt': No space left on device</code>
would cause the script to abort. Instead, since the function is invoked
in an <code>if</code> statement, the script will proceed to delete all
the files even though it failed to back them up.</p>
<p>The fix is to call it outside of an <code>if</code> statement. There
is no point in checking whether the command succeeded, since the script
would abort if it didn't. You may also want to consider replacing
<code>set -e</code> with explicit <code>|| exit</code> after every
relevant command to avoid such surprises.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>If you don't care that the function runs without <code>set -e</code>,
you can disable this warning.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li>BashFaq #105: <a href="https://mywiki.wooledge.org/BashFAQ/105">Why
doesn't set -e (or set -o errexit, or trap ERR) do what I
expected?</a></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


